/**
 * @copyright SilverBiology, LLC
 * @author SilverBiology, LLC
 * @website http://www.silverbiology.com
*/

Ext.ns("BioBlitz");

BioBlitz.GeoInfoPanel = function(config){

	this.btnLocation = {
			scope: this
		,	iconCls: 'locate'
		,	handler: this.getCurrentLocation
	}
		
	if (Ext.is.iPad) {
		this.btnLocations.text= 'Get Location';
	}
	
	Ext.apply(this, config, { 
			scroll: 'vertical'
		,	title: 'GeoLocation'
		,	draggable : false
		,	items: [{
					xtype: 'fieldset'
				,	title: 'Geographical Information'
	//                    instructions: 'Please enter the information above.',
				,	defaults: {
							required: true
						,	labelAlign: 'left'
						,	draggable: false
					}
				,	items: [{
							xtype: 'textfield'
						,	ref: '../txtLat'
						,	name : 'lat'
						,	label: 'Latitude'
						,	id: 'latitude'
						,	disabled: true
					}, {
							xtype: 'textfield'
						,	ref: '../txtLon'
						,	name : 'lon'
						,	label: 'Longitude'
						,	id: 'longitude'
						,	disabled: true
					}]
			}, {
					xtype: 'fieldset'
				,	title: 'Taxanomic Information'
	//                    instructions: 'Please enter the information above.',
				,	defaults: {
							required: true
						,	labelAlign: 'left'
						,	draggable : false
					}
				,	items: [{
							xtype: 'textfield'
						,	name : 'genus'
						,	label: 'Genus'
						,	id: 'genus'
					}, {
							xtype: 'textfield'
						,	name : 'species'
						,	label: 'Species'
						,	id: 'species'
					}]
			}]
		,	dockedItems: [{
					xtype: 'toolbar'
				,	dock: 'bottom'
				,	draggable : false
				,	defaults: {
							iconMask: true
						,	ui: 'plain'
					}
				,	items: [ 
							this.btnLocation
						,	{
							xtype: 'spacer'
						},{
								text: 'Reset'
							,	iconCls: 'refresh'	
							,	scope: this	
							,	handler: this.resetform
						},{
								text: 'Save'
							,	id: 'save'
							,	iconCls: 'tag'
							,	scope: this
							,	handler: this.saveRecord
						}
					]
			}]
	});

	BioBlitz.GeoInfoPanel .superclass.constructor.call(this,config);
}

Ext.extend(BioBlitz.GeoInfoPanel , Ext.form.FormPanel, {

		saveRecord: function() {
			
			var params = this.getValues();
			params.name = localStorage.getItem("name");
			console.log(params);
			
			Ext.Ajax.request({
					url: 'resources/api.php'
				,	params: params
				,	scope: this
				,	success: function(response, opts) {
						this.reset();
						alert("Data Added!");
					}
			}, form);
			this.fireEvent("loadDataToMap",this);
		}

	,	locationFound: function(position) {
			Ext.getCmp('latitude').setValue( position.coords.latitude );
			Ext.getCmp('longitude').setValue( position.coords.longitude );							
			this.fireEvent("locationFound", position.coords);
		}

	,	locationNotFound: function(error) {
			alert("Location Not Found");
			this.fireEvent("locationNotFound", this);
/*				
				if (Ext.getCmp('latitude').getValue() == "" && Ext.getCmp('longitude').getValue() == "") {
//					this.txtLat.setValue('30');//'N/A'); 
					Ext.getCmp('longitude').setValue('-90');//'N/A');
				}
				switch(error.code) {
					case 3:
						break;

					case 1:
						navigator.geolocation.getCurrentPosition(locationFound, locationNotFound, options);
						break;

					case 2:
					//		navigator.geolocation.getCurrentPosition(locationFound, locationNotFound, options);
						break;
				}
*/				
		}

	,	getCurrentLocation: function() {
		
			var options = {
					timeout: 10000
				,	maximumAge: 20000
				,	enableHighAccuracy: true
			};

			if (navigator.geolocation) {  
				navigator.geolocation.getCurrentPosition( this.locationFound.createDelegate(this), this.locationNotFound.createDelegate(this), options );
			} else {  
				alert("I'm sorry, but geolocation services are not supported by your browser.");  
			}
		}

	,	resetform : function(){
			this.reset();
			this.fireEvent('resetmap',this);
		}

});